package com.android.music2;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;

public class RatingDialog extends Dialog implements OnRatingBarChangeListener, OnClickListener{

	private RatingBar ratingBar;
	private Button okButton;
		
	public RatingDialog(Context context) {
		super(context);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.rating_dialog);
				
		ratingBar = (RatingBar) findViewById(R.id.rating_dialog_rating_bar);
		ratingBar.setStepSize(1);		
		ratingBar.setOnRatingBarChangeListener(this);

		okButton = (Button) findViewById(R.id.rating_dialog_ok_button);				
		okButton.setOnClickListener(this);
		
	}

	@Override
	public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
		if(!fromUser) return;
		//Caller will set rating via a call to getRating() when the dialog has been dismissed
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.rating_dialog_ok_button){
			dismiss();
		}
	}
	
	public void setInitialRating(float rating) {
		ratingBar.setRating(rating);
	}
	
	public void setTrackName(CharSequence trackName) {
		setTitle("Select Rating for " + trackName);
	}
	
	public float getRating() {
		return ratingBar.getRating();
	}

}

